#ifndef __TOOLS_H__
#define __TOOLS_H__

#include <windows.h>

#ifdef __GNUC__

#define _atom_add _atom_add_gnu
#define _atom_inc _atom_inc_gnu
#define _atom_sub _atom_sub_gnu
#define _atom_dec _atom_dec_gnu

#else

#define _atom_add _atom_add_intel
#define _atom_inc _atom_inc_intel
#define _atom_sub _atom_sub_intel
#define _atom_dec _atom_dec_intel

#endif

#define _atom_add_gnu(v1,v2) __asm__("movl %1,%%ebx":"=a"(v1)::"eax");\
			 __asm__("lock addl %1,%%ebx":"=a"(v2):"eax");\
			 __asm__("movl %%ebx,%1"::"m"(v1));

#define _atom_inc_gnu(v1)	 __asm__("movl %1,%%ebx":"=a"(v1)::"eax");\
			 __asm__("lock incl %%ebx");\
			 __asm__("movl %%ebx,%1"::"m"(v1));

#define _atom_sub_gnu(v1,v2) __asm__("movl %1,%%ebx":"=a"(v1)::"eax");\
			 __asm__("lock subl %1,%%ebx":"=a"(v2):"eax");\
			 __asm__("movl %%ebx,%1"::"m"(v1));

#define _atom_dec_gnu(v1)	 __asm__("movl %1,%%ebx":"=a"(v1)::"eax");\
			 __asm__("lock decl %%ebx");\
			 __asm__("movl %%ebx,%1"::"m"(v1));

#define _atom_add_intel(v1,v2) _asm{\
					push eax;\
					mov eax,v1;\
					lock add eax,v2;\
					mov v1,eax;\
					pop eax;\
				    };

#define _atom_inc_intel(v1) _asm{\
					push eax;\
					mov eax,v1;\
					lock inc eax;\
					mov v1,eax;\
					pop eax;\
				 };

#define _atom_sub_intel(v1,v2) _asm{\
					push eax;\
					mov eax,v1;\
					lock sub eax,v2;\
					mov v1,eax;\
					pop eax;\
				   };

#define _atom_dec_intel(v1) _asm{\
					push eax;\
					mov eax,v1;\
					lock dec eax;\
					mov v1,eax;\
					pop eax;\
				};


#endif //__TOOLS_H__
